//设置物流信息
$(".table").eq(0).on("click",".check_post",function(e){
	var id = $(this).parents(".operate").parents(".order_detail").attr("id"),
		box = $(this).parents(".operate").find(".post_detail"),
		load = box.attr("data-load");
			if(load=="false"){
				getPostDetail(id);
			}else{
				box.fadeIn();
			}
		e.stopPropagation();
})
//名字详情
$(".product_name").mouseover(function(){
	$(this).attr("title",$(this).text())
})
//获取数据
function getPostDetail(id){
	var order_id = id;
	var box = $("#"+order_id).find(".post_detail");
	$.ajax({
		url:base_url + "admin/findKuaiDiMsg",
		data:{
			id:order_id
		}
	}).done(function(data){
		var package = data.data;
		console.log(data);
		if(package.length>0&&data.code=="0"){
			listPostDetail(package,order_id);
		}else{
			box.html("<p style='width:200px;text-align:center;margin:5px 0;'>木有信息哦</p>")
		}
 		box.attr("data-load","true");
 		box.fadeIn();
	})
}
//铺数据
function listPostDetail(result,order_id){
	var html = '',
		box = $("#"+order_id).find(".post_detail");
		console.log(result);
	for (var i = 0;i<result.length;i++) {
		
		var package = JSON.parse(result[i].msg);
		var com = package.com,
			nu = package.nu,
			address = package.data,
			pac = i+1;
			tr = '';
			if(package.status=="200"){
				for (var j=0;j<address.length;j++) {
					var start_time = address[j].time,
						context = address[j].context,
						status = 'status2';
					if(address.length>0){
						(j==address.length-1)&&(status='status4');
						tr += 
								'<tr>'+
									'<td class="post_time">'+start_time+'</td>'+
									'<td class="post_status '+status+'"></td>'+
									'<td>'+address[j].context+'</td>'+
								'</tr>'
					}
				}
			html+= '<div class="package">'+
						'<p class="detail_head clearfix">'+
							'<span class="col-4">'+result[i].company+'</span>'+
							'运单编号：'+nu+
							'<span  class="post_page">'+
								'<i class="iconfont pre">&#xe619;</i>'+
								'<b class="cur">'+pac+'</b>/<b class="tol">'+result.length+'</b>'+
								'<i class="iconfont nex">&#xe61a;</i>'+
							'</span>'+
						'</p>'+
						'<p class="info_head clearfix">'+
							'<span class="col-4">时间</span>'+
							'<span class="col-8">地址</span>'+
						'</p>'+
						'<div class="info_detail">'+
							'<table class="table">'+
								tr+
							'</table>'+
						'</div>'+
					'</div>'
			}else{
				html+='<div class="package" style="width:300px;text-align:center">'+
							"该包裹木有数据哦~"+
							'<span  class="post_page">'+
									'<i class="iconfont pre">&#xe619;</i>'+
									'<b class="cur">'+pac+'</b>/<b class="tol">'+result.length+'</b>'+
									'<i class="iconfont nex">&#xe61a;</i>'+
							'</span>'+
					  '</div>'
			}
	}
	box.append(html);
	box.find(".package").eq(0).show();
}
//
$(".post_detail").on("click",".pre",function(){
	changePage($(this),-1)
})
$(".post_detail").on("click",".nex",function(){
	changePage($(this),1)
})

function changePage(box,page){
	var _this = box.parents(".package"),
	     cur = parseInt(_this.find(".cur").text()),
	     tol = parseInt(_this.find(".tol").text()),
	     go = cur + page-1;
	     if(go==tol){
	     	go=0;
	     }else if(go==-1){
	     	go=tol-1;
	     }
	var  goBox =  _this.parent(".post_detail").find(".package").eq(go);
	     console.log(go);
	     console.log(cur);
	     console.log(tol);
	     _this.hide();
	     goBox.fadeIn();
}
$(document).click(function(){
	$(".post_detail").hide();
})
$(".post_detail").click(function(e){
	e.stopPropagation();
})